
# Makefile - dgsh - shell like bash
# Autor        Diego Gomes da Silva    nUSP 7656467
#              Alfredo Lima Porfirio   nUSP 7236727
# Sistemas Operacionais 2
# Professor Monaco
# ICMC/USP

# Caminhos para que encontre corretamente os arquivos

INCLUDE_DIR=${CURDIR}/include
LIB_DIR=${CURDIR}/lib
SOURCE_DIR=${CURDIR}/src
BUILD_DIR=${CURDIR}/build
BIN_DIR=${CURDIR}/bin

# variaveis e flags para o compilador
CC = gcc
LIBS=
LDFLAGS=-L$(LIB_DIR) $(LIBS) 
CFLAGS = -I$(INCLUDE_DIR) -Wall --std=c99 --pedantic -D_POSIX_SOURCE --pedantic-errors  $(LDFLAGS)

#programa a ser construido
TARGET=$(BIN_DIR)/dgsh

CSOURCES = $(wildcard $(SOURCE_DIR)/*.c)
VPATH=$(SOURCE_DIR)
OBJ=$(join $(addsuffix ../build/, $(dir $(CSOURCES))), $(notdir $(CSOURCES:.c=.o))) 
INCLUDES=$(join $(addsuffix ../include/, $(dir $(CSOURCES))), $(notdir $(CSOURCES:.c=.h)))
DEPENDS=$(join $(addsuffix ../build/, $(dir $(CSOURCES))), $(notdir $(CSOURCES:.c=.d)))



all: $(TARGET)
	
	@true


clean:
	@-rm -f $(TARGET) $(OBJ) $(DEPENDS) *~ $(INCLUDE_DIR)/*~ $(SOURCE_DIR)/*~


$(TARGET): $(OBJ)
	@mkdir -p $(dir $@)
	
	@echo "Linkando o objeto $(notdir $@) ..."
	
	@$(CC)   $(CFLAGS) -o $@ $^ $(LIBS)
	@echo Objeto linkado!
	

$(SOURCE_DIR)/../build/%.o : %.c 
	@mkdir -p $(dir $@)
	@echo "Compilando $(notdir $<) ..."
	
	@$(CC) $(CFLAGS) -c $< -o $@


$(SOURCE_DIR)/../build/%.d : %.c  
	@mkdir -p $(dir $@)
	@echo Verificando dependencias do arquivo $*.c
	@$(CC) -M $(CFLAGS) $<   > $@

	
-include $(DEPENDS)


